package com.wzcl.app.controller;

import com.wzcl.app.dao.model.Commodity;
import com.wzcl.app.dao.model.Course;
import com.wzcl.app.dao.model.Material;
import com.wzcl.app.dao.model.common.Base;
import com.wzcl.app.model.common.*;
import com.wzcl.app.model.dto.CommodityDto;
import com.wzcl.app.model.dto.FindCommodityDto;
import com.wzcl.app.model.vo.BaseVo;
import com.wzcl.app.model.vo.CommodityVo;
import com.wzcl.app.model.vo.CourseVo;
import com.wzcl.app.model.vo.MaterialVo;
import com.wzcl.app.service.CommodityService;
import com.wzcl.app.service.CourseService;
import com.wzcl.app.service.MaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/commodity")
@Api(value="商品管理controller",tags={"商品管理接口"})
public class CommodityController{

    @Resource
    private CommodityService service;

    @Resource
    private CourseService courseService;

    @Resource
    private MaterialService materialService;

    @ApiOperation("添加商品")
    @PostMapping("/add")
    public RVO<CommodityVo> addCommodity(@RequestBody @Valid CommodityDto commodityDto){
        Commodity commodity = service.addCommodity(commodityDto);
        return RVO.success(CommodityVo.create(commodity));
    }

    @ApiOperation("修改商品")
    @PostMapping("/update")
    public RVO<CommodityVo> updateCoursePoint(@RequestBody @Valid CommodityDto commodityDto) {
        Commodity commodity = service.updateCommodity(commodityDto);
        return RVO.success(CommodityVo.create(commodity));
    }


    @ApiOperation("获取商品")
    @PostMapping("/get")
    public RVO<CommodityVo> getCommodity(@RequestBody CommodityDto commodityDto){
        Commodity commodity = service.getCommodityById(commodityDto.getId());
        if(commodity !=null){
            return RVO.success(CommodityVo.create(commodity));
        }
        return RVO.success(null);
    }

    @ApiOperation("商品信息列表list")
    @PostMapping("/getList")
    public RVO<List<CommodityVo>> getCoursePointList(@RequestBody CommodityDto commodityDto){
        List<Commodity> list = service.getCommodityList(commodityDto);
        if (list != null && list.size() > 0) {
            List<CommodityVo> collect = list.stream().map(CommodityVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("商品信息列表page")
    @PostMapping("/getPage")
    public  RVO<List<CommodityVo>> getCoursePointPage(@RequestBody CommodityDto commodityDto){
        MyPage<Commodity> page = service.getCommodityPage(commodityDto);
        if (page != null && page.getContent().size()>0) {
            List<CommodityVo> collect = page.getContent().stream().map(CommodityVo::create).collect(Collectors.toList());
            return RVO.success(collect,page);
        }
        return RVO.success(null);
    }

    @ApiOperation("商品列表page")
    @PostMapping("/getCommodity")
    public  RVO<List<BaseVo>> findCommodity(@RequestBody @Valid FindCommodityDto findCommodityDto){
        MyPage page;
        if(findCommodityDto.getType().equals(WZCL.COMMODITY_TYPE.COURSE)){
            page = courseService.getCoursePage(findCommodityDto.getCourseDto());
        }else if(findCommodityDto.getType().equals(WZCL.COMMODITY_TYPE.MATERIAL)){
            page = materialService.getMaterialPage(findCommodityDto.getMaterialDto());
        }else{
            throw new BusinessException(CodeMsg.COMMODITY_TYPE_ERROR);
        }
        if(page != null && page.getContent().size()>0){
            ArrayList<BaseVo> vos = new ArrayList<>();
            for (Object resource : page.getContent()) {
                if(resource instanceof Course)
                    vos.add(CourseVo.create((Course) resource));
                else
                    vos.add(MaterialVo.create((Material)resource));
            }
            return RVO.success(vos,page);
        }
        return RVO.success(null);
    }

    @ApiOperation("绑定活动")
    @PostMapping("/bindActivity")
    public RVO<CommodityVo> bindCommodity(@RequestBody CommodityDto commodityDto){
        Commodity commodity = service.bindActivity(commodityDto);
        return RVO.success(CommodityVo.create(commodity));
    }

    @ApiOperation("商品上下线")
    @PostMapping("/sale")
    public RVO<CommodityVo> saleCommodity(@RequestBody CommodityDto commodityDto){
        if(commodityDto==null || commodityDto.getId()==null||commodityDto.getIsSale()==null){
            throw new BusinessException(CodeMsg.MUST_BE_NOT_NULL,"缺少操作类型");
        }
        if(commodityDto.getIsSale()){
            return RVO.success(CommodityVo.create(service.saleCommodity(commodityDto.getId())));
        }else {
            return RVO.success(CommodityVo.create(service.unSaleCommodity(commodityDto.getId())));
        }
    }


    @ApiOperation("获取商品真实数据")
    @PostMapping("/getResource")
    public RVO<BaseVo> getResource(@RequestBody CommodityDto commodityDto){
        Base base = service.getResource(commodityDto);
        if(base instanceof Course)
            return RVO.success(CourseVo.create((Course) base));
        if(base instanceof Material)
            return RVO.success(MaterialVo.create((Material) base));
        return RVO.success(null);
    }
}
